#include "AIState.h"
#include "Behaviour.h"
#include "AIAgent.h"


#include <GL\glut.h>

AIState::AIState(AIAgent* agent)  : m_pAgent(agent)
{

}

Vec3f AIState::SumForces()
{
	Vec3f forces;
	std::map<Behaviour*, float>::iterator it = m_behaviourMap.begin();
	std::map<Behaviour*, float>::iterator end = m_behaviourMap.end();


	for( ; it != end ; ++it)
	{

		Vec3f force = it->first->Update();
		float weight = it->second;

		forces += force * weight;

	}


	return forces;
}

void AIState::DebugDrawForces()
{

	std::map<Behaviour*, float>::iterator it = m_behaviourMap.begin();
	std::map<Behaviour*, float>::iterator end = m_behaviourMap.end();

	/*
	glLineWidth(2.5f);
	glBegin(GL_LINES);

	glVertex3f(0, 0, 0);
	glVertex3f(0, 0, -8);

	glEnd();
	*/
	glDisable(GL_LIGHTING);
	glDisable(GL_TEXTURE_2D);

	glLineWidth(2.5f);
	for( ; it != end ; ++it)
	{
		glBegin(GL_LINES);
		Vec3f arrowColour = it->first->m_debugDrawColour;
		glColor3f(arrowColour.X, arrowColour.Y, arrowColour.Z);
		Vec3f force = it->first->Update();
		float weight = it->second;

		//Coords are in local space
		//Vec3f agentPos = m_pAgent->GetPos();
		Vec3f agentPos(0,0,0);
		Vec3f forcePos = agentPos + (force * weight);

		glVertex3f(agentPos.X, agentPos.Y, agentPos.Z);
		glVertex3f(forcePos.X, forcePos.Y, forcePos.Z);

		//Arrow heads
		
		glEnd();
	}
	
	glEnable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);

}

void AIState::InsertBehaviour(Behaviour* behaviour, float weight)
{
	m_behaviourMap.insert(BehaviourAndWeight(behaviour, weight));
}


BehaviourAndWeightMap* AIState::GetBehaviours() 
{ 
	return &m_behaviourMap; 
}

AIState::~AIState()
{
	std::map<Behaviour*, float>::iterator it = m_behaviourMap.begin();
	std::map<Behaviour*, float>::iterator end = m_behaviourMap.end();

	for( ; it != end; ++it)
	{
		delete it->first;

	}
	m_behaviourMap.clear();
}